﻿

var parentTabUrl = "";
var currentTabUrl = "";
var menusJson;
var maxWindows = 8; //最大显示窗口数量
var tabUrls = new Array(); //tab的url集合
var isLock = false; //是否锁定窗口

//init
$(function () {

    //自适应窗口大小
    //window.setInterval("reinitIframe()", 200);
})
//设置导航
function setNavName(title, objhref) {
    $("#navName").attr("href", objhref);
    $("#navName").text(title);
}

//刷新父级页面
function refreshParentForm() {
    var tabs = document.getElementById("divTab").childNodes;
    var parentTabUrl = tabs[currentTabUrl].parentTabUrl == undefined ? -1 : tabs[currentTabUrl].parentTabUrl;


    alert(-1);
    if (parentTabUrl != -1) {

        var frame = window.frames[parentTabUrl];

        var btnRefresh = frame.document.getElementById("btnRefresh");
        btnRefresh.click();
    }
}

//选择菜单
function selectedMenu(src) {
    if (src.url == "") return;

    src.className = "selectedMenu";
    addTab(src.tabName, src.url);
}

//关闭当前tab
function closeCurrentTab() {
    closeTab(currentTabUrl);
}

function showTabContainer(flag) {
    flag = true;

    if (flag == "true" || flag == true)
        $("#tabContainer").show();
    else
        $("#tabContainer").hide();
}

//关闭tab
function closeTab(url) {
    isLock = false; //关闭窗口，则解锁

    var frameContainers = $(".frameContainer");
    var tabs = $("#ulTabs li");
    var closeRefresh = false; //是否需要刷新上级页面

    tabs.each(function (i) {
        if ($(this).attr("url") == url) {
            closeRefresh = $(this).attr("closeRefresh");

            $(this).remove();
            frameContainers.eq(i).remove();

            //移除集合中的路径
            for (var n = tabUrls.length - 1; n >= 0; n--) {
                if (tabUrls[n] == url) {
                    tabUrls.splice(n, n);
                }
            }
        }
    })

    if ($("#ulTabs li").length == 1) {  //如果窗口全部关闭则显示主页
        var url = $("#ulTabs li").eq(0).attr("url")

        selectedTab(url);
        return;
    }

    var lastUrl = tabUrls.pop();
    if (lastUrl != undefined)
        selectedTab(lastUrl, closeRefresh); //重新选中
}

//添加mid窗口
function addMidWindow(name, url, visible) {
    addTab(name, url, visible, currentTabUrl);
}

//显示模态窗口  showDialogToolbar 是否显示窗口工具条
function showModalDialog(url, closeRefresh, option) {
    var opt = { name: "", url: url, showHead: false, isModal: true, closeRefresh: closeRefresh };
    $.extend(opt, option);

    addTab2(opt);
}

//显示窗口
function showWindows(title, url, showHead, option) {
    showHead = false;
    var opt = { name: title, url: url, template: 1, showHead: showHead, isModal: false, parentTabUrl: "", refresh: false, closeRefresh: false };
    $.extend(opt, option);

    addTab2(opt);
}




/*增加tab
//Option = {
Template:1, 模板
ShowHead:true|false,  是否显示tab横栏
}

*/
function addTab2(option) {
    if (isLock) return;

    /*默认属性

    refresh : 每次点击是否刷新
    closeRefresh : 关闭后是否刷新父页面

    */
    var opt = { name: "", url: "", template: 1, showHead: true, isModal: false, parentTabUrl: "", refresh: false, closeRefresh: false, toolbar: true };
    $.extend(opt, option);


    //查看tab是否已存在，如果存在则显示
    var ulTabs = $("#ulTabs");
    var liTabs = $("#ulTabs li");

    for (var i = 0; i < liTabs.length; i++) {
        if (liTabs.eq(i).attr("url") == opt.url) {
            selectedTab(opt.url);
            return;
        }
    }

    //检测窗口大于最大数量，则关闭打开时间最长的窗口
    if (liTabs.length > maxWindows) {
        closeTab(liTabs.eq(1).attr("url"));
    }

    var liTabTemplate = $("#liTabTemplate");
    var newTab = liTabTemplate.clone();
    newTab.attr("url", opt.url);
    newTab.attr("parentTabUrl", opt.parentTabUrl);
    newTab.attr("isModal", opt.isModal);
    newTab.attr("showHead", opt.showHead);
    newTab.attr("refresh", opt.refresh);
    newTab.attr("template", opt.template);
    newTab.attr("closeRefresh", opt.closeRefresh);

    var aTab = $(".tabName", newTab);
    //aTab.text(opt.name);
    $(".title", aTab).text(opt.name);
    aTab.click(function () {
        selectedTab(opt.url);
    });

    ulTabs.append(newTab);
    newTab.show();

    addFrame(opt); //添加内容区
    selectedTab(opt.url);
}


/*增加tab,该方法将陆续取消
*/
function addTab(name, url, isModal) {
    isModal = true;
    var opt = { name: name, url: url, showHead: !isModal };
    addTab2(opt);
}

//select tab action
//refresh 是否需要刷新
function selectedTab(url, refresh) {
    if (isLock) return;

    var frames = $(".frameContainer");
    var tabs = $("#ulTabs li");

    var midTabIndex = -1;  //子级tab

    tabUrls.push(url); //把url加入集合数组，用于记录顺序

    parentTabUrl = currentTabUrl; //设置回上一层次的URL
    currentTabUrl = url; //当前选中的URL

    //设置为非选中
    for (var i = 0; i < tabs.length; i++) {
        var tab = tabs.eq(i);

        if (tabs.eq(i).attr("url") == url) {
            tabs.eq(i).addClass("selectedTab");
            frames.eq(i).show(); //显示框架内容

            var isModal = tabs.eq(i).attr("isModal");
            var showHead = tabs.eq(i).attr("showHead");
            showTabContainer(showHead);

            if (isModal == "true")
                isLock = true; //模态窗口，锁定

            //是否刷新
            if (tab.attr("refresh") == "true" || tab.attr("refresh") == true || refresh == "true" || refresh == true) {

                //是否刷新
                var frame = $(".frm", frames.eq(i)).eq(0);

                var tmpUrl = tabs.eq(i).attr("url");
                if (tmpUrl.indexOf("?") == -1)
                    tmpUrl += "?rnd=" + Math.random();
                else
                    tmpUrl += "&rnd=" + Math.random();

                if (frame.attr("tagName") == "IFRAME") {

                    var objFrame = window.frames[frame.attr("id")];
                    var btn = objFrame.document.getElementById("btnSearch");
                    var btnRefresh = objFrame.document.getElementById("btnRefresh");

                    if (btnRefresh != undefined) {
                        $(btnRefresh).trigger("click")
                    }
                    else if (btn != undefined) {
                        $(btn).trigger("click")
                    }
                    else {
                        frame.attr("src", tmpUrl);
                    }
                }
                else {//DIV
                    frame.html("Loading......");
                    //加载数据
                    $.ajax({
                        type: "GET",
                        url: tmpUrl,
                        context: frame,
                        success: function (msg) {
                            //alert(msg);
                            frame.html(msg);
                            //$(this).html(msg);
                        }
                    });
                }
            }
        }
        else {
            tabs.eq(i).removeClass("selectedTab");
            tabs.eq(i).addClass("unselectedTab");
            frames.eq(i).hide(); //隐藏框内容
        }
    }
}

function addFrame(option) {
    var frameContainerTemplate = $("#frameContainerTemplate");
    if (option.template == 2) {
        frameContainerTemplate = $("#frameContainerTemplate2");
    }

    var newFrameContainer = frameContainerTemplate.clone();
    var newFrame = $(".frm", newFrameContainer).eq(0);
    newFrame.attr("src", option.url);
    newFrame.attr("id", "ifm" + Math.random());

    if (newFrame.attr("tagName") == "IFRAME") {
        newFrame.attr("height", "650");
        newFrame.css("height", "650");
    }

    if (option.toolbar == false)  //不显示工具条
        $(".line_top", newFrameContainer).hide();

    newFrameContainer.attr("id", "");
    $("#divContent").append(newFrameContainer);
}

//设置窗口高度
function setDialogHeight(height) {
    var containers = $(".frameContainer:visible");
    if (containers.length == 0) return;

    var container = containers.eq(0);
    var iframe = $(".frm", container).eq(0);

    iframe.height(height);
    container.height(height + 25);
}

function reinitIframe() {
    var containers = $(".frameContainer:visible");
    if (containers.length == 0) return;

    var container = containers.eq(0);
    var iframe = $(".frm", container).eq(0);

    if (iframe.attr("tagName") == "IFRAME") {
        var height = iframe.contents().find("#container").height();
        if (height == null)
            height = iframe.height();

        if (height < 650)
            height = 650;

        iframe.height(height);
        container.height(height + 45);
    }

    $("#container").css("height", "");
    var bHeight = $("body").height();
    var cHeight = $("#container").height();

    if (cHeight < bHeight) {
        $("#container").css("height", bHeight);
    }
}

function scrollTop() {
    $("body").scrollTop(0);
}



//打开编辑窗口           
function showModifyPwdForm() {
    var url = "/Accounts/ModifyPwd.aspx";
    var set = { scrollbars: "auto", menubar: "no", toolbar: "no" }
    Win.Open(url, 350, 150, set);

    return false;
}

